package com.ruoyi.system.service.impl;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ClueCustomer;
import com.ruoyi.system.domain.vo.ClueCustomerVO;
import com.ruoyi.system.mapper.ClueCustomerMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IClueCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 线索客户Service业务层处理
 *
 * @author luojiqiang
 * @date 2025-05-15
 */
@Service
public class ClueCustomerServiceImpl implements IClueCustomerService {
    @Autowired
    private ClueCustomerMapper clueCustomerMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询线索客户
     *
     * @param id 线索客户主键
     *
     * @return 线索客户
     */
    @Override
    public ClueCustomer selectClueCustomerById(Long id) {
        return clueCustomerMapper.selectClueCustomerById(id);
    }

    /**
     * 查询线索客户列表
     *
     * @param clueCustomer 线索客户
     *
     * @return 线索客户
     */
    @Override
    public List<ClueCustomerVO> selectClueCustomerList(ClueCustomer clueCustomer) {
        List<ClueCustomerVO> clueCustomerVOS = clueCustomerMapper.selectClueCustomerList(clueCustomer);
        if (!CollectionUtils.isEmpty(clueCustomerVOS)) {
            List<Long> teacherIds = clueCustomerVOS.stream().map(ClueCustomerVO::getTeacherId).filter(Objects::nonNull).collect(Collectors.toList());
            List<SysUser> sysUsers = sysUserMapper.selectByIds(teacherIds);
            if (!CollectionUtils.isEmpty(sysUsers)) {
                Map<Long, SysUser> teacherMap = sysUsers.stream().collect(Collectors.toMap(SysUser::getUserId, v -> v, (k1, k2) -> k2));
                clueCustomerVOS.forEach(e -> {
                    SysUser sysUser = teacherMap.get(e.getTeacherId());
                    if (Objects.nonNull(sysUser)) {
                        e.setTeacherName(sysUser.getUserName());
                    }

                });
            }
        }

        return clueCustomerVOS;
    }

    /**
     * 新增线索客户
     *
     * @param clueCustomer 线索客户
     *
     * @return 结果
     */
    @Override
    public int insertClueCustomer(ClueCustomer clueCustomer) {
        clueCustomer.setCreateTime(DateUtils.getNowDate());
        return clueCustomerMapper.insertClueCustomer(clueCustomer);
    }

    /**
     * 修改线索客户
     *
     * @param clueCustomer 线索客户
     *
     * @return 结果
     */
    @Override
    public int updateClueCustomer(ClueCustomer clueCustomer) {
        clueCustomer.setUpdateTime(DateUtils.getNowDate());
        return clueCustomerMapper.updateClueCustomer(clueCustomer);
    }

    /**
     * 批量删除线索客户
     *
     * @param ids 需要删除的线索客户主键
     *
     * @return 结果
     */
    @Override
    public int deleteClueCustomerByIds(Long[] ids) {
        return clueCustomerMapper.deleteClueCustomerByIds(ids);
    }

    /**
     * 删除线索客户信息
     *
     * @param id 线索客户主键
     *
     * @return 结果
     */
    @Override
    public int deleteClueCustomerById(Long id) {
        return clueCustomerMapper.deleteClueCustomerById(id);
    }
}
